﻿/*
	Copyright 2010 Intuitive Solutions

	Licensed under the Apache License, Version 2.0 (the "License");
	you may not use this file except in compliance with the License.
	You may obtain a copy of the License at

	   http://www.apache.org/licenses/LICENSE-2.0

	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
*/

using System;
using Crochet.Core;
using System.Threading;
using Crochet.Core.Attributes;

namespace Crochet.Examples.HelloWorld
{
	class Program
	{
		protected int iTimes = 0, iTimesAsync = 0;

		static void Main(string[] args)
		{
			Program pTestProgram = new Program();

			//Schedule process using Attributes.
			CrochetManager.Instance.Dispose();
			CrochetManager.Instance.Schedule(new SchedulerDelegate(pTestProgram.FailingMethod));
			CrochetManager.Instance.Dispose();

			//Schedule process without using Attributes.
			CrochetManager.Instance.Schedule(SchedulerType.ThreadPool, "Async10", new SchedulerDelegate(pTestProgram.AsynchronousMethod));
			CrochetManager.Instance.Schedule(SchedulerType.ThreadPool, "Async10", new SchedulerDelegate(pTestProgram.FailingAsynchronousMethod));

			//Schedule process using Attributes.
			CrochetManager.Instance.Schedule(new SchedulerDelegate(pTestProgram.TimerMethod));

			CrochetManager.Instance.Dispose();
			//Schedule process using Attributes.
			CrochetManager.Instance.Schedule(new SchedulerDelegate(pTestProgram.SequentialMethod));
			CrochetManager.Instance.Schedule(new SchedulerDelegate(pTestProgram.SequentialMethod));
			CrochetManager.Instance.Schedule(new SchedulerDelegate(pTestProgram.SequentialMethod));
			CrochetManager.Instance.Dispose();
			System.Console.WriteLine("6 - HelloWorld program.  Everything Ok.");
		}

		[SchedulerAttribute(SchedulerType.Immediate, "Retry")]
		public void FailingMethod(IScheduler sSourceScheduler)
		{
			System.Console.Write("1 - Failing Method.");

			if (iTimes++ < 2)
			{
				System.Console.WriteLine("  Retrying...");
				throw new Exception();
			}
			else
			{
				System.Console.WriteLine("  Done...");
			}
		}

		public void AsynchronousMethod(IScheduler sSourceScheduler)
		{
			for (int i = 0; i < 10; i++)
			{
				System.Console.WriteLine("2 - Asynchronous Method.  Everything Ok.");

				Thread.Sleep(500);
			}
			CrochetManager.Instance.Dispose();
		}

		public void FailingAsynchronousMethod(IScheduler sSourceScheduler)
		{
			System.Console.Write("3 - Failing Asynchronous Method.");

			if (iTimesAsync++ < 2)
			{
				System.Console.WriteLine("  Retrying...");
				throw new Exception();
			}
			else
			{
				System.Console.WriteLine("  Done...");
			}
		}

		[SchedulerAttribute(SchedulerType.TimerPool, "Timer")]
		public void TimerMethod(IScheduler sSourceScheduler)
		{
			System.Console.WriteLine("4 - Timer Method.  Everything Ok.");
		}

		[SchedulerAttribute(SchedulerType.Sequential, "Sequential")]
		public void SequentialMethod(IScheduler sSourceScheduler)
		{
			System.Console.WriteLine("5 - Sequential Method.  Everything Ok.  " + (iTimes++));
		}
	}
}
